package com.ctrlcar.giftrecord.controller;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ctrlcar.giftrecord.dto.GiftBookDTO;
import com.ctrlcar.giftrecord.pojo.GiftBook;
import com.ctrlcar.giftrecord.pojo.ReceiveGift;
import com.ctrlcar.giftrecord.pojo.model.Result;
import com.ctrlcar.giftrecord.service.GiftBookService;
import com.ctrlcar.giftrecord.service.ReceiveGiftService;
import com.ctrlcar.giftrecord.vo.GiftBookListVo;
import com.ctrlcar.giftrecord.vo.GiftBookVo;
import com.ctrlcar.giftrecord.vo.ReceiveGiftVo;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.Random;

/**
 * @Author:
 * @Description: 礼簿
 * @DateTime: 2025-03-26 19:37
 **/
@CrossOrigin(origins = "*")
@RestController
@RequestMapping("/giftBook")
public class GiftBookController {

    @Resource
    private GiftBookService giftBookService;
    @Resource
    private ReceiveGiftService receiveGiftService;



    /**
     * 创建礼簿
     * @param giftBookDTO
     * @return
     */
    @PostMapping("/createAndUpdate")
    private Result<String> createGiftBook(@RequestBody GiftBookDTO giftBookDTO) {
        if(giftBookDTO.getUserId() == null){
            return Result.error("用户未找到");
        }
        if (giftBookDTO.getBookName() == null) {
            return Result.error("请输入礼簿名");
        }
        if (giftBookDTO.getBookId() == null || giftBookDTO.getBookId() == 0){
            //新增
            // 根据参数创建礼簿
            GiftBook giftBook = new GiftBook();
            giftBook.setUserId(giftBookDTO.getUserId());
            giftBook.setBookName(giftBookDTO.getBookName());
            giftBook.setRemark(giftBookDTO.getRemark());
            giftBook.setCreateTime(giftBookDTO.getCreateTime());
            // 随机生成6位数，并设置为授权码
            giftBook.setAuthCode(generateAuthCode());
            // 其他字段可以根据需要进行设置，例如设置默认值或从其他地方获取
            giftBook.setUpdateTime(null); // 示例：更新时间可以设置为null或其他默认值
            giftBook.setIsDelete(0); // 示例：删除标志可以设置为0或其他默认值
            //写入数据库
            boolean save = giftBookService.save(giftBook);
            if (save){
                return Result.success("创建成功");
            }
            return Result.error("创建失败");
        }
        // 修改
        GiftBook giftBook = giftBookService.getById(giftBookDTO.getBookId());
        if (giftBook == null) {
            return Result.error("礼簿未找到");
        }
        // 更新礼簿信息
        giftBook.setUserId(giftBookDTO.getUserId());
        giftBook.setBookName(giftBookDTO.getBookName());
        giftBook.setRemark(giftBookDTO.getRemark());
        //修改创建日期
        giftBook.setCreateTime(giftBookDTO.getCreateTime());
        giftBook.setUpdateTime(new Date()); // 更新时间为当前时间
        boolean update = giftBookService.updateById(giftBook);
        if (update) {
            return Result.success("修改成功");
        }
        return Result.error("修改失败");
    }

    /**
     * 根据userId查询List<GiftBookListVo>
     * @return
     */
    @GetMapping("/list")
    private Result<List<GiftBookListVo>> list(@RequestParam Integer userId ){
        return giftBookService.getGiftBookList(userId);
    }

    /**
     * 根据bookId查询单个GiftBook
     * @param bookId
     * @return
     */
    @GetMapping("/get")
    private Result<GiftBookVo> get(@RequestParam Integer bookId , @RequestParam(required = false) String sel) {
        GiftBook giftBook = giftBookService.getById(bookId);
        if (giftBook == null) {
            return Result.error("礼簿未找到");
        }
//        根据礼簿id查询该礼簿的送礼人
        List<ReceiveGiftVo> list = receiveGiftService.getReceiveGiftByBookId(giftBook.getBookId(),sel);
//        封装vo
        GiftBookVo giftBookVo = new GiftBookVo();
        BeanUtil.copyProperties(giftBook,giftBookVo);
        giftBookVo.setReceiveGiftsVo(list);
        return Result.success(giftBookVo);
    }

    /**
     * 根据bookId删除GiftBook
     * @param bookId
     * @return
     */
    @DeleteMapping("/delete")
    private Result<String> delete(@RequestParam Integer bookId) {
//        GiftBook giftBook = giftBookService.getById(bookId);
//        if (giftBook == null) {
//            return Result.error("礼簿未找到");
//        }
//        boolean delete = giftBookService.removeById(bookId);
//        if (delete) {
//            return Result.success("删除成功");
//        }
        return giftBookService.delBookById(bookId);
    }

    /**
     * 根据authCode查询单个GiftBook
     * @param authCode
     * @return
     */
    @GetMapping("/getByAuthCode")
    private Result<GiftBookVo> get(@RequestParam String authCode) {
        LambdaQueryWrapper<GiftBook> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(GiftBook::getAuthCode, authCode);
        GiftBook giftBook = giftBookService.getOne(queryWrapper);
        if (giftBook == null) {
            return Result.error("礼簿未找到");
        }
//        根据礼簿id查询该礼簿的送礼人
        List<ReceiveGiftVo> list = receiveGiftService.getReceiveGiftByBookId(giftBook.getBookId(), null);
//        封装vo
        GiftBookVo giftBookVo = new GiftBookVo();
        BeanUtil.copyProperties(giftBook,giftBookVo);
        giftBookVo.setReceiveGiftsVo(list);
        return Result.success(giftBookVo);
    }
    /**
     * 生成6位随机授权码
     * @return 6位随机数字字符串
     */
    private String generateAuthCode() {
        // 获取当前时间的纳秒部分作为种子
        long nanoTime = System.nanoTime();
        // 创建随机数生成器
        Random random = new Random(nanoTime);
        // 生成一个6位随机数字
        int randomNumber = random.nextInt(900000) + 100000;
        return String.valueOf(randomNumber);
    }

}
